/**************************************************************************
 * @doc ICIC
 * @module ICICISO.C |
 * Source code for the ICIC Receipt handling functions.
 *
 * Product         :   SoftPay 2000 (Global Payment Methodology).
 * <nl>Developer   :   SoftPay Development Team (I_SP2000).
 * <nl>Notes       :
 *
 * @head3 ICIC Functions |
 * @index | ICIC
 * @end
 *
 * Copyright (c) 1996-2000 by VeriFone Inc. All rights reserved.
 *
 * No part of this software may be used, stored, compiled, reproduced,
 * modified, transcribed, translated, transmitted, or transferred, in any form
 * or by any means whether electronic, mechanical, magnetic, optical,
 * or otherwise, without the express prior written permission of VeriFone, Inc.
 *
 * Revision History
 * ------------------------------------------------------------------------
 $Log:
 ********************************************************************************/
#ifdef IMAM_LOG
#ifndef LOGSYS_FLAG
#define LOGSYS_FLAG
#endif
#endif

#include <project.h>

#include <aclconio.h>
#include <txoinc.h>
#include <ascii.h>
#include <errno.h>
#include <applidl.h>
#include <printer.h>
#include <formater.h>
#include <message.h>
#include <stdlib.h>
#include <aclstr.h>
#include <string.h>
#include <actutil.h>
#include <acldev.h>

#include <define.h>
#include <sizes.h>
#include <transact.h>
#include <print.h>
#include <gds.h>
#include <lct.h>
#include <tct.h>
#include <pct.h>
#include <iit.h>
#include <ui.h>
#include <msg.h>
#include <msgfuncs.h>
#include <form.h>
#include <stats.h>
#include <paramrpt.h>
#include <logo.h>
#include <tran.h>
#include <hdt.h>
#include <report.h>
#include <mit.h>
#include <msgclrk.h>
#include <report.h>
#include <pform.h>
#include <format.h>
#include <settlbat.h>
#include <multilng.h>
#include <settle.h>
#include <clrkrpts.h>
#include <date.h>
#include <funcids.h>
#include <batmsg.h>
#include <trt.h>
#include <pbatch.h>
#include <aclstr.h>
#include <batch.h>
#include <tblutil.h> //CPAC2.1B
#include <spdebug.h>
#include <init.h>
#include <util.h>
#include <confio.h>
#include <oper.h>
#include <entry.h>
#include <file.h>
#include <card.h>
#include <date.h>
#include <accum.h>
#include <accmsg.h>
#include <rcptsegs.h>
#include <rcptcmpt.h>
#include <udt.h>
#include <table.h>
#include <ifldtbl.h>

#include <EMVTags.hpp>
#include <EMVTypeDefs.hpp>
#include <EMVCWrappers.h>
#include <EMVResult.hpp>
#include <Vxemvapdef.h>

#include "..\CPACsrc\CPACids.h"
#include "..\cpacsrc\sqautils.h"

#include "..\EMVSrc\EMVsrcdef.h"
#include "..\EMVSrc\EMVids.h"
#include "..\EMVSrc\EMViso.h"

extern unsigned char szEMVField55[EMV_FIELD55_SIZE + 1];
extern int inGlobalNumberScripts;


 void vdLoadEMVField22(TRANSACTION_OBJECT *pobTran)
{
	////////////////////////////
	int amexFlag = 0;
	char szPanLo[20];
	char szPanHi[20];
	memset(szPanLo,0x00,sizeof(szPanLo));
	memset(szPanHi,0x00,sizeof(szPanHi));
	strncpy(szPanLo,szGetPANLo(),4);
	strncpy(szPanHi,szGetPANHi(),4);
	if(atoi(szPanLo) >= 3400 && atoi(szPanHi) <= 3499) amexFlag = 1;
	if(atoi(szPanLo) >= 3700 && atoi(szPanHi) <= 3799) amexFlag = 1;
	////////////////////////////	

    if (!fGetImamEmvHost()) {
		vdLoadCPACField22(pobTran);
		return;
    }
	if (pobTran->srBRec.inTranCardType == DEBIT_CARD)
	{
		if ((pobTran->srBRec.fManual == VS_TRUE) && (pobTran->inFallbackAllowed == VS_FALSE))
			strlcpy((char *)srISO.pszField22, MANUAL_POS_DEBIT_ENTRY_CODE, POS_EM_SIZE);
		else if ((pobTran->srBRec.fManual == VS_TRUE) && (pobTran->inFallbackAllowed == VS_TRUE))
			strlcpy((char *)srISO.pszField22, CHIP_MANUAL_POS_DEBIT_ENTRY_CODE, POS_EM_SIZE);
		else if((pobTran->srBRec.inChipStatus == EMV_CARD)) 
			strlcpy((char*)srISO.pszField22, CHIP_POS_ENTRY_CODE, POS_EM_SIZE);
		else if (pobTran->srBRec.inChipStatus == IMAM_MSD_TAP_CARD)
			strlcpy((char*)srISO.pszField22, IMAM_MSD_TAP_CARD_ENTRY_MODE, POS_EM_SIZE);  //PAYPASS
		else if (pobTran->srBRec.inChipStatus == IMAM_CHIP_TAP_CARD)
			strlcpy((char*)srISO.pszField22, IMAM_CHIP_TAP_CARD_ENTRY_MODE, POS_EM_SIZE);  //VISAWAVE
		else if((pobTran->szServiceCode[0] == '2') || (pobTran->szServiceCode[0] == '6'))
			strlcpy((char*)srISO.pszField22, CHIP_SWIPE_POS_DEBIT_ENTRY_CODE, POS_EM_SIZE);
		else
			strlcpy((char *)srISO.pszField22, SWIPE_POS_DEBIT_ENTRY_CODE, POS_EM_SIZE);
	}
	else
	{	//edi EMV field 22
		if(amexFlag==1){ //amex
			if ((pobTran->srBRec.fManual == VS_TRUE) && (pobTran->inFallbackAllowed == VS_FALSE)){
				if(fGetImamManual4DBC()) strlcpy((char *)srISO.pszField22, CHIP_MANUAL_AMEX4DBC_POS_ENTRY_CODE , POS_EM_SIZE); 
				else strlcpy((char *)srISO.pszField22, CHIP_MANUAL_AMEX_POS_ENTRY_CODE, POS_EM_SIZE);
			}
			else if ((pobTran->srBRec.fManual == VS_TRUE) && (pobTran->inFallbackAllowed == VS_TRUE)){
				if(fGetImamManual4DBC()) strlcpy((char *)srISO.pszField22, CHIPFALLBACK_MANUAL_AMEX4DBC_POS_ENTRY_CODE , POS_EM_SIZE); 
				else strlcpy((char *)srISO.pszField22, CHIPFALLBACK_MANUAL_AMEX_POS_ENTRY_CODE , POS_EM_SIZE);
			}
			else if ((pobTran->srBRec.fManual == VS_FALSE) && (pobTran->inFallbackAllowed == VS_TRUE)
				&& fGetImamSwipe4DBC()){
				strlcpy((char *)srISO.pszField22, CHIPFALLBACK_SWIPE_AMEX4DBC_POS_ENTRY_CODE, POS_EM_SIZE); 
			}
			else if ((pobTran->srBRec.fManual == VS_FALSE) && (pobTran->inFallbackAllowed == VS_FALSE)
				&& fGetImamSwipe4DBC()&&(pobTran->srBRec.inChipStatus == 0)){
				strlcpy((char *)srISO.pszField22, CHIP_SWIPE_AMEX4DBC_POS_ENTRY_CODE, POS_EM_SIZE); 
			}
			else if(((pobTran->srBRec.inChipStatus == EMV_CARD) || (pobTran->srBRec.inChipStatus == EMV_EASY_ENTRY_CARD)) 
				&&((pobTran->srTRTRec.TransactionCode != REFUND)&& (pobTran->srTRTRec.TransactionCode != REFUND_OFFLINE)
				&& (pobTran->srTRTRec.TransactionCode != SALE_OFFLINE)))//KiaSoon 070206 Modified: For non-full EMV chip txn 
				strlcpy((char*)srISO.pszField22, CHIP_AMEX_POS_ENTRY_CODE, POS_EM_SIZE);
			else if (pobTran->srBRec.inChipStatus == IMAM_MSD_TAP_CARD)
				strlcpy((char*)srISO.pszField22, IMAM_MSD_TAP_CARD_ENTRY_MODE, POS_EM_SIZE);  //PAYPASS
			else if (pobTran->srBRec.inChipStatus == IMAM_CHIP_TAP_CARD)
				strlcpy((char*)srISO.pszField22, IMAM_CHIP_TAP_CARD_ENTRY_MODE, POS_EM_SIZE);  //VISAWAVE
			else if(((pobTran->szServiceCode[0] == '2') || (pobTran->szServiceCode[0] == '6'))
				&&((pobTran->srTRTRec.TransactionCode != REFUND)&& (pobTran->srTRTRec.TransactionCode != REFUND_OFFLINE)
				&& (pobTran->srTRTRec.TransactionCode != SALE_OFFLINE)))
				strlcpy((char*)srISO.pszField22, CHIPFALLBACK_SWIPE_AMEX_POS_ENTRY_CODE, POS_EM_SIZE);//CHIPFALLBACK_SWIPE_AMEX_POS_ENTRY_CODE
			else
				strlcpy((char *)srISO.pszField22, AMEX_SWIPE_POS_ENTRY_CODE, POS_EM_SIZE);
		}else{ //visa master
			if ((pobTran->srBRec.fManual == VS_TRUE) && (pobTran->inFallbackAllowed == VS_FALSE))
				strlcpy((char *)srISO.pszField22, MANUAL_POS_ENTRY_CODE, POS_EM_SIZE);
			else if ((pobTran->srBRec.fManual == VS_TRUE) && (pobTran->inFallbackAllowed == VS_TRUE))
				strlcpy((char *)srISO.pszField22, CHIP_MANUAL_POS_ENTRY_CODE, POS_EM_SIZE);
			else if((pobTran->srBRec.inChipStatus == EMV_CARD) || (pobTran->srBRec.inChipStatus == EMV_EASY_ENTRY_CARD))//KiaSoon 070206 Modified: For non-full EMV chip txn 
				strlcpy((char*)srISO.pszField22, CHIP_POS_ENTRY_CODE, POS_EM_SIZE);
			else if (pobTran->srBRec.inChipStatus == IMAM_MSD_TAP_CARD)
				strlcpy((char*)srISO.pszField22, IMAM_MSD_TAP_CARD_ENTRY_MODE, POS_EM_SIZE);  //PAYPASS
			else if (pobTran->srBRec.inChipStatus == IMAM_CHIP_TAP_CARD)
				strlcpy((char*)srISO.pszField22, IMAM_CHIP_TAP_CARD_ENTRY_MODE, POS_EM_SIZE);  //VISAWAVE
			else if((pobTran->szServiceCode[0] == '2') || (pobTran->szServiceCode[0] == '6'))
				strlcpy((char*)srISO.pszField22, CHIP_SWIPE_POS_DEBIT_ENTRY_CODE, POS_EM_SIZE);
			else
				strlcpy((char *)srISO.pszField22, SWIPE_POS_DEBIT_ENTRY_CODE, POS_EM_SIZE);

		}
	}
}

void vdLoadEMVField55(TRANSACTION_OBJECT *pobTran)
{
	unsigned char bTagOutput[255];
	unsigned short usLength;
	int inTotalLength = 0, testlength = 0, testlength3 = 0;
	unsigned char szTextBuff[10+1]; //Renu
	unsigned char szHexBuff[10+1]; //Renu

	////////////////////////////
	int amexFlag = 0;
	char szPanLo[20];
	char szPanHi[20];
	memset(szPanLo,0x00,sizeof(szPanLo));
	memset(szPanHi,0x00,sizeof(szPanHi));
	strncpy(szPanLo,szGetPANLo(),4);
	strncpy(szPanHi,szGetPANHi(),4);
	if(atoi(szPanLo) >= 3400 && atoi(szPanHi) <= 3499) amexFlag = 1;
	if(atoi(szPanLo) >= 3700 && atoi(szPanHi) <= 3799) amexFlag = 1;
	////////////////////////////	

	memset(szEMVField55, NULL_CH, sizeof(szEMVField55));


    if( ((pobTran->srBRec.inCode==SALE) && (pobTran->srTRTRec.TransactionCode==SALE)) ||
		 ((pobTran->srBRec.inCode==AUTH_ONLY) && (pobTran->srTRTRec.TransactionCode==AUTH_ONLY)) ||
		 ((pobTran->srBRec.inCode==IMAM_CARDVER) && (pobTran->srTRTRec.TransactionCode==IMAM_CARDVER)) ||
		 ((pobTran->srBRec.inCode==PRE_AUTH) && (pobTran->srTRTRec.TransactionCode==PRE_AUTH)) ||
		 ((pobTran->srBRec.inChipStatus == 0)/*&&(pobTran->srBRec.inCode==CITI_EPP_CASHBACK) && (pobTran->srTRTRec.TransactionCode==CITI_EPP_CASHBACK)*/) //edi - chip ga perlu isi 4dbc
	   ) {
	   if ( ((pobTran->srBRec.fManual==VS_TRUE) && (fGetImamManual4DBC())) ||
			 ((pobTran->srBRec.fManual==VS_FALSE) && (fGetImamSwipe4DBC()))
			){
				memset((char *)szEMVField55, 0x00, sizeof(szEMVField55));
				memcpy((char *)szEMVField55, "\x00\x04", 2);
				strcpy((char *)&szEMVField55[2], pobTran->szCVV2Value);
				return;
		}
	}

	if((pobTran->srBRec.inChipStatus == IMAM_CHIP_TAP_CARD) && 
	   ((pobTran->srTRTRec.TransactionCode == SALE_OFFLINE) || (pobTran->srTRTRec.TransactionCode == SALE))
	){
		unsigned char szChipData[256];
		vdImamGetCTLData(&pobTran->srBRec, szChipData);
		inTotalLength = szChipData[0];
		sprintf((char *)szTextBuff,"%04d", inTotalLength);
		SVC_DSP_2_HEX((char *)szTextBuff, (char *)szHexBuff,2);
		memcpy(szEMVField55,szHexBuff, 2);
		memcpy(&szEMVField55[2], &szChipData[1], inTotalLength);		
		return;
   	}


	if(pobTran->srBRec.inChipStatus != 0)
	{
	//	ACTIVITY_LOG("Field55 Entered!");
		memset(bTagOutput, 0x00, sizeof(bTagOutput));
		if(amexFlag==1) inCITIBuildEMV_AMEXHostData(pobTran,(unsigned char *)bTagOutput, &usLength); //edi - amex EMV
		else inCITIBuildEMVHostData(pobTran,(unsigned char *)bTagOutput, &usLength); //edi
	//	ACTIVITY_LOG("BuildEmvHostData COMP");
		inTotalLength = usLength;
		sprintf((char *)szTextBuff,"%04d", inTotalLength);
		SVC_DSP_2_HEX((char *)szTextBuff, (char *)szHexBuff,2);
		memcpy(szEMVField55,szHexBuff, 2);
		memcpy(&szEMVField55[2], &bTagOutput[0], inTotalLength);
	//	ACTIVITY_LOG("Copy to field55 ok!");

	}
	else
	{
		memset(srISO.pszField55, 0x00, FIELD55_SIZE);
	}

}


/* Edi Collect all the EMV data elements that need to be sent to the host in Field 55*/
int inCITIBuildEMV_AMEXHostData(TRANSACTION_OBJECT *pobTran,unsigned char *pbtBuff, unsigned short *pusLen)
{
	int inOffset = 0;
	unsigned short usLen;
	unsigned char btTmp[22];
	int inSize = 0;
	int inOffsetBefore = 0;

	///Edi///
	char szTextBuff[2+1];
       char szHexBuff[1+1];
	   
	char buff1[50];
	char buff2[50];
	char szHexBuffer1[50];
	char szHexBuffer2[50];
	
	memset(buff1,0x00,sizeof(buff1));
	memset(buff2,0x00,sizeof(buff2));
	memset(szHexBuffer1,0x00,sizeof(szHexBuffer1));
	memset(szHexBuffer2,0x00,sizeof(szHexBuffer2));

	strcpy(buff1,"C1C7D5E2");
	strcpy(buff2,"0001");
	SVC_DSP_2_HEX(buff1, szHexBuffer1,4);
	SVC_DSP_2_HEX(buff2, szHexBuffer2,2);

	////////

	memcpy(&pbtBuff[inOffset],szHexBuffer1,4);
	inOffset += 4;
	memcpy(&pbtBuff[inOffset],szHexBuffer2,2);
	inOffset += 2;
	
	if((pobTran->fReferral == VS_TRUE) && (pobTran->inEMVCardDecision == EMV_OFFLINE_AUTH))
 // KiaSoon: 070206: For CALL REFERRAL CASE, UPLOAD THE 1st ONLINE TXN
	{
		LOG_PRINTF(("DE55-1"));
		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_82_AIP_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_82_AIP, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_95_TVR_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_95_TVR, inSize, &pbtBuff[inOffset]);
		
		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9A_TXN_DATE_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9A_TXN_DATE, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9C_TXN_TYPE_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9C_TXN_TYPE, inSize, &pbtBuff[inOffset]);
		
		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F02_AMT_AUTH_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F02_AMT_AUTH, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F03_AMT_OTHR_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F03_AMT_OTHR, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F09_APPLN_VER_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F09_APPLN_VER, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F10_ISSUER_APP_DATA_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F10_ISSUER_APP_DATA, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F1A_TERM_CRTY_CODE_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F1A_TERM_CRTY_CODE, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F1E_IFD_SER_NUM_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F1E_IFD_SER_NUM, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F26_APP_CRYPTOGRAM_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F26_APP_CRYPTOGRAM, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F27_CID_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F27_CID, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F33_TERM_CAPABILITIES_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F33_TERM_CAPABILITIES, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F34_CVM_RESULTS_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F34_CVM_RESULTS, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F35_TERM_TYPE_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F35_TERM_TYPE, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F36_ATC_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F36_ATC, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F37_UNPREDICT_NUMBER_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F37_UNPREDICT_NUMBER, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F41_TRANS_SEQ_COUNTER_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F41_TRANS_SEQ_COUNTER, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F53_TRAN_CAT_CODE_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F53_TRAN_CAT_CODE, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_5F2A_CURR_CODE_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_5F2A_CURR_CODE, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_84_DF_NAME_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_84_DF_NAME, inSize, &pbtBuff[inOffset]);

		// commented out for spqalog038
		//inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_ISSUER_SCRIPT_RESULTS_LEN);
		//inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_ISSUER_SCRIPT_RESULTS, inSize, &pbtBuff[inOffset]);
		*pusLen = (unsigned short)inOffset;

	}
	
	else if((pobTran->srBRec.inCode != BATCH_UPLOAD) && 
		(pobTran->srTRTRec.TransactionCode != PRE_COMP)&&
		(pobTran->srTRTRec.TransactionCode != VOID) &&
		(pobTran->srBRec.inCode != TC_UPLOAD)
		){
		LOG_PRINTF(("DE55-2"));
		if (usEMVGetTLVFromColxn(TAG_9F03_AMT_OTHER_NUM, btTmp, &usLen) != EMV_SUCCESS)
		{	// This DO seems to be needed even if it does not exist
			memset(btTmp, 0, 6);
			usEMVUpdateTLVInCollxn(TAG_9F03_AMT_OTHER_NUM, btTmp, 6);
			LOG_PRINTFF((0x08L, "TAG(0x%X): EMVUpdateTLV[TAG_9F03_AMT_OTHER_NUM = with NULL]", TAG_9F03_AMT_OTHER_NUM));
		} else {
			{
			char buff[256];
			memset(buff, 0x00, sizeof(buff));
			if (usLen<128) hex_2_asc((BYTE *)buff, btTmp, usLen);
			LOG_PRINTFF((0x08L, "TAG(0x%X): EMVGetTLV[TAG_9F03_AMT_OTHER_NUM = %s]", TAG_9F03_AMT_OTHER_NUM, buff));
			}
		}

		inOffsetBefore = inOffset;
		inOffset += inAddDataToEMV_AMEXPacket(TAG_9F26_APPL_CRYPTOGRAM, &pbtBuff[inOffset]);	
		inSize = inOffset - inOffsetBefore;
		if (inSize>0){ // : Store for Batch Upload
			inStoreBatchFieldData ((BATCH_REC *)&pobTran->srBRec, FID_EMV_TAG_9F26_APP_CRYPTOGRAM_LEN_REQ, (unsigned char*)&inSize, sizeof(int));
			inStoreBatchFieldData ((BATCH_REC *)&pobTran->srBRec, FID_EMV_TAG_9F26_APP_CRYPTOGRAM_REQ,  (unsigned char*)&pbtBuff[inOffsetBefore], inSize);
		}

		inOffsetBefore = inOffset;
		inOffset += inAddDataToEMV_AMEXPacket2(TAG_9F10_ISSUER_APP_DATA, &pbtBuff[inOffset]);
		inSize = inOffset - inOffsetBefore;
		if (inSize>0){ // : Store for Batch Upload
			inStoreBatchFieldData ((BATCH_REC *)&pobTran->srBRec, FID_EMV_TAG_9F10_ISSUER_APP_DATA_LEN_REQ, (unsigned char*)&inSize, sizeof(int));
			inStoreBatchFieldData ((BATCH_REC *)&pobTran->srBRec, FID_EMV_TAG_9F10_ISSUER_APP_DATA_REQ,  (unsigned char*)&pbtBuff[inOffsetBefore], inSize);
		}
		
		inOffset += inAddDataToEMV_AMEXPacket(TAG_9F37_UNPRED_NUM, &pbtBuff[inOffset]);

		inOffset += inAddDataToEMV_AMEXPacket(TAG_9F36_ATC, &pbtBuff[inOffset]);

		inOffsetBefore = inOffset;
		inOffset += inAddDataToEMV_AMEXPacket(TAG_9500_TVR, &pbtBuff[inOffset]);
		inSize = inOffset - inOffsetBefore;
		if (inSize>0){ // : Store for Batch Upload
			inStoreBatchFieldData ((BATCH_REC *)&pobTran->srBRec, FID_EMV_TAG_95_TVR_LEN_REQ, (unsigned char*)&inSize, sizeof(int));
			inStoreBatchFieldData ((BATCH_REC *)&pobTran->srBRec, FID_EMV_TAG_95_TVR_REQ,  (unsigned char*)&pbtBuff[inOffsetBefore], inSize);
		}

		inOffset += inAddDataToEMV_AMEXPacket(TAG_9A_TRAN_DATE, &pbtBuff[inOffset]);
		
		inOffset += inAddDataToEMV_AMEXPacket(TAG_9C00_TRAN_TYPE, &pbtBuff[inOffset]);

		inOffset += inAddDataToEMV_AMEXPacket(TAG_9F02_AMT_AUTH_NUM, &pbtBuff[inOffset]);
		//jrd 05182005
		inOffset += inAddDataToEMV_AMEXPacket(TAG_5F2A_TRANS_CURCY_CODE, &pbtBuff[inOffset]);
		
		inOffset += inAddDataToEMV_AMEXPacket(TAG_9F1A_TERM_COUNTY_CODE, &pbtBuff[inOffset]);
		
		inOffset += inAddDataToEMV_AMEXPacket(TAG_8200_APPL_INTCHG_PROFILE, &pbtBuff[inOffset]);

		inOffset += inAddDataToEMV_AMEXPacket(TAG_9F03_AMT_OTHER_NUM, &pbtBuff[inOffset]);

		inOffset += inAddDataToEMV_AMEXPacket(TAG_5F34_APPL_PAN_SEQNUM, &pbtBuff[inOffset]);

		inOffset += inAddDataToEMV_AMEXPacket(TAG_9F27_CRYPT_INFO_DATA, &pbtBuff[inOffset]);

		if (usEMVGetTLVFromColxn(TAG_4F_AID, btTmp, &usLen) == EMV_SUCCESS)
		{//special AID needs to be concatenated with length (LLVAR) first (hex, 1 byte) then AID
			memset(szTextBuff,0x00, sizeof(szTextBuff));
			memset(szHexBuff,0x00, sizeof(szHexBuff));
			sprintf((char *)szTextBuff,"%02d", usLen);           //Renu
			SVC_DSP_2_HEX((char *)szTextBuff, (char *)szHexBuff,1);
			memcpy(pbtBuff+inOffset, szHexBuff, 1);
			memcpy(pbtBuff+inOffset+1, btTmp, usLen);
			inOffset +=(usLen+1);
		}
		
		inOffset += inAddDataToEMV_AMEXPacket(TAG_9F09_TERM_VER_NUM, &pbtBuff[inOffset]);
		
		inOffset += inAddDataToEMV_AMEXPacket(TAG_9F34_CVM_RESULTS, &pbtBuff[inOffset]);

		inOffset += inAddDataToEMV_AMEXPacket(TAG_9F0E_IAC_DENIAL, &pbtBuff[inOffset]);
		inOffset += inAddDataToEMV_AMEXPacket(TAG_9F0F_IAC_ONLINE, &pbtBuff[inOffset]);
		inOffset += inAddDataToEMV_AMEXPacket(TAG_9F0D_IAC_DEFAULT, &pbtBuff[inOffset]);

		
		///////////////////////////////////////////////////////////////////////////////
		

		//LeeChyi - 29122050: START
		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_ISSUER_SCRIPT_RESULTS_LEN);
//		if (inSize > 0)
//			inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_ISSUER_SCRIPT_RESULTS, inSize, &pbtBuff[inOffset]);
		if (inSize > 0)
		{
			unsigned char chScriptTmp[25];
			inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_ISSUER_SCRIPT_RESULTS, inSize, chScriptTmp);
			inOffset +=inBuildTLVfromData(&pbtBuff[inOffset], 0x9F5B, inSize, chScriptTmp);
		}
		//LeeChyi - END


		*pusLen = (unsigned short)inOffset;
	}
	else if((pobTran->srBRec.inCode == BATCH_UPLOAD) ||
		(pobTran->srTRTRec.TransactionCode == PRE_COMP)||
		(pobTran->srTRTRec.TransactionCode == VOID) ||
		(pobTran->srBRec.inCode == TC_UPLOAD)
		) {
		LOG_PRINTF(("DE55-3"));

#ifdef IMAM_PERMATA
		if (pobTran->srBRec.inCode == BATCH_UPLOAD){
			inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F26_APP_CRYPTOGRAM_LEN_REQ);
			inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F26_APP_CRYPTOGRAM_REQ, inSize, &pbtBuff[inOffset]);
		} else 
#endif
		{
			inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F26_APP_CRYPTOGRAM_LEN);
			inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F26_APP_CRYPTOGRAM, inSize, &pbtBuff[inOffset]);
		}

		//if (pobTran->srBRec.inCode == BATCH_UPLOAD){
		{	LOG_PRINTFF((0x0L, "TAG_9F10 : NOT SENT in VOID, etc")); //LIEM
			//inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F10_ISSUER_APP_DATA_LEN);
			//inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F10_ISSUER_APP_DATA, inSize, &pbtBuff[inOffset]);
			inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F10_ISSUER_APP_DATA_LEN_REQ);
			inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F10_ISSUER_APP_DATA_REQ, inSize, &pbtBuff[inOffset]);
		}

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F37_UNPREDICT_NUMBER_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F37_UNPREDICT_NUMBER, inSize, &pbtBuff[inOffset]);
		
		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F36_ATC_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F36_ATC, inSize, &pbtBuff[inOffset]);
		
		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_95_TVR_LEN_REQ);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_95_TVR_REQ, inSize, &pbtBuff[inOffset]);
			
		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9A_TXN_DATE_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9A_TXN_DATE, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9C_TXN_TYPE_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9C_TXN_TYPE, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F02_AMT_AUTH_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F02_AMT_AUTH, inSize, &pbtBuff[inOffset]);

		//jrd 05232005
		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_5F2A_CURR_CODE_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_5F2A_CURR_CODE, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F1A_TERM_CRTY_CODE_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F1A_TERM_CRTY_CODE, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_82_AIP_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_82_AIP, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F03_AMT_OTHR_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F03_AMT_OTHR, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_5F34_PAN_SEQ_NUM_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_5F34_PAN_SEQ_NUM, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F27_CID_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F27_CID, inSize, &pbtBuff[inOffset]);

		//9F06	
		inSize = inGetDataLengthFromBatch(pobTran,FID_EMV_TAG_4F_AID_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_4F_AID, inSize, &pbtBuff[inOffset]);
		
		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F09_APPLN_VER_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F09_APPLN_VER, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F34_CVM_RESULTS_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F34_CVM_RESULTS, inSize, &pbtBuff[inOffset]);

		//9F0E.. 9F0F.. 9F0D
		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_IAC_DENIAL_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_IAC_DENIAL, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_IAC_ONLINE_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_IAC_ONLINE, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_IAC_DEFAULT_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_IAC_DEFAULT, inSize, &pbtBuff[inOffset]);

		//////////////////////////////////////////////////////////////
		// commented out for spqalog038
		
		//LeeChyi - 29122050 - START
		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_ISSUER_SCRIPT_RESULTS_LEN);
//		if (inSize > 0)
//			inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_ISSUER_SCRIPT_RESULTS, inSize, &pbtBuff[inOffset]);
		if (inSize > 0)
		{
			unsigned char chScriptTmp[25];
			inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_ISSUER_SCRIPT_RESULTS, inSize, chScriptTmp);
			inOffset +=inBuildTLVfromData(&pbtBuff[inOffset], 0x9F5B, inSize, chScriptTmp);
		}
		//LeeChyi - END

		*pusLen = (unsigned short)inOffset;
	}

	return VS_SUCCESS;
}

/* Collect all the EMV data elements that need to be sent to the host in Field 55*/
int inCITIBuildEMVHostData(TRANSACTION_OBJECT *pobTran,unsigned char *pbtBuff, unsigned short *pusLen)
{
	int inOffset = 0;
	unsigned short usLen;
	unsigned char btTmp[22];
	int inSize = 0;
	int inOffsetBefore = 0;


	if((pobTran->fReferral == VS_TRUE) && (pobTran->inEMVCardDecision == EMV_OFFLINE_AUTH))
 // KiaSoon: 070206: For CALL REFERRAL CASE, UPLOAD THE 1st ONLINE TXN
	{
		LOG_PRINTF(("DE55-1"));
		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_82_AIP_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_82_AIP, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_95_TVR_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_95_TVR, inSize, &pbtBuff[inOffset]);
		
		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9A_TXN_DATE_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9A_TXN_DATE, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9C_TXN_TYPE_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9C_TXN_TYPE, inSize, &pbtBuff[inOffset]);
		
		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F02_AMT_AUTH_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F02_AMT_AUTH, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F03_AMT_OTHR_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F03_AMT_OTHR, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F09_APPLN_VER_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F09_APPLN_VER, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F10_ISSUER_APP_DATA_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F10_ISSUER_APP_DATA, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F1A_TERM_CRTY_CODE_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F1A_TERM_CRTY_CODE, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F1E_IFD_SER_NUM_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F1E_IFD_SER_NUM, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F26_APP_CRYPTOGRAM_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F26_APP_CRYPTOGRAM, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F27_CID_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F27_CID, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F33_TERM_CAPABILITIES_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F33_TERM_CAPABILITIES, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F34_CVM_RESULTS_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F34_CVM_RESULTS, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F35_TERM_TYPE_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F35_TERM_TYPE, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F36_ATC_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F36_ATC, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F37_UNPREDICT_NUMBER_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F37_UNPREDICT_NUMBER, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F41_TRANS_SEQ_COUNTER_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F41_TRANS_SEQ_COUNTER, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F53_TRAN_CAT_CODE_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F53_TRAN_CAT_CODE, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_5F2A_CURR_CODE_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_5F2A_CURR_CODE, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_84_DF_NAME_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_84_DF_NAME, inSize, &pbtBuff[inOffset]);

		// commented out for spqalog038
		//inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_ISSUER_SCRIPT_RESULTS_LEN);
		//inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_ISSUER_SCRIPT_RESULTS, inSize, &pbtBuff[inOffset]);
		*pusLen = (unsigned short)inOffset;

	}
	
	else if((pobTran->srBRec.inCode != BATCH_UPLOAD) && 
		(pobTran->srTRTRec.TransactionCode != PRE_COMP)&&
		(pobTran->srTRTRec.TransactionCode != VOID) &&
		(pobTran->srBRec.inCode != TC_UPLOAD)
		){
		LOG_PRINTF(("DE55-2"));
		if (usEMVGetTLVFromColxn(TAG_9F03_AMT_OTHER_NUM, btTmp, &usLen) != EMV_SUCCESS)
		{	// This DO seems to be needed even if it does not exist
			memset(btTmp, 0, 6);
			usEMVUpdateTLVInCollxn(TAG_9F03_AMT_OTHER_NUM, btTmp, 6);
			LOG_PRINTFF((0x08L, "TAG(0x%X): EMVUpdateTLV[TAG_9F03_AMT_OTHER_NUM = with NULL]", TAG_9F03_AMT_OTHER_NUM));
		} else {
			{
			char buff[256];
			memset(buff, 0x00, sizeof(buff));
			if (usLen<128) hex_2_asc((BYTE *)buff, btTmp, usLen);
			LOG_PRINTFF((0x08L, "TAG(0x%X): EMVGetTLV[TAG_9F03_AMT_OTHER_NUM = %s]", TAG_9F03_AMT_OTHER_NUM, buff));
			}
		}

		inOffset += inAddDataToEMVPacket(TAG_8200_APPL_INTCHG_PROFILE, &pbtBuff[inOffset]);
		//inOffset += inAddDataToEMVPacket(TAG_84_DF_NAME, &pbtBuff[inOffset]);  //jrd 05182005
		inOffsetBefore = inOffset;
		inOffset += inAddDataToEMVPacket(TAG_9500_TVR, &pbtBuff[inOffset]);
		inSize = inOffset - inOffsetBefore;
		if (inSize>0){ // : Store for Batch Upload
			inStoreBatchFieldData ((BATCH_REC *)&pobTran->srBRec, FID_EMV_TAG_95_TVR_LEN_REQ, (unsigned char*)&inSize, sizeof(int));
			inStoreBatchFieldData ((BATCH_REC *)&pobTran->srBRec, FID_EMV_TAG_95_TVR_REQ,  (unsigned char*)&pbtBuff[inOffsetBefore], inSize);
		}
		inOffset += inAddDataToEMVPacket(TAG_9A_TRAN_DATE, &pbtBuff[inOffset]);
		inOffset += inAddDataToEMVPacket(TAG_9C00_TRAN_TYPE, &pbtBuff[inOffset]);
		//inOffset += inAddDataToEMVPacket(TAG_5F2A_TRANS_CURCY_CODE, &pbtBuff[inOffset]); //jrd 05182005
		inOffset += inAddDataToEMVPacket(TAG_9F02_AMT_AUTH_NUM, &pbtBuff[inOffset]);
		inOffset += inAddDataToEMVPacket(TAG_9F03_AMT_OTHER_NUM, &pbtBuff[inOffset]);
		inOffset += inAddDataToEMVPacket(TAG_9F09_TERM_VER_NUM, &pbtBuff[inOffset]);
		
		inOffsetBefore = inOffset;
		inOffset += inAddDataToEMVPacket(TAG_9F10_ISSUER_APP_DATA, &pbtBuff[inOffset]);
		inSize = inOffset - inOffsetBefore;
		if (inSize>0){ // : Store for Batch Upload
			inStoreBatchFieldData ((BATCH_REC *)&pobTran->srBRec, FID_EMV_TAG_9F10_ISSUER_APP_DATA_LEN_REQ, (unsigned char*)&inSize, sizeof(int));
			inStoreBatchFieldData ((BATCH_REC *)&pobTran->srBRec, FID_EMV_TAG_9F10_ISSUER_APP_DATA_REQ,  (unsigned char*)&pbtBuff[inOffsetBefore], inSize);
		}
		inOffset += inAddDataToEMVPacket(TAG_9F1A_TERM_COUNTY_CODE, &pbtBuff[inOffset]);
		inOffset += inAddDataToEMVPacket(TAG_9F1E_IFD_SER_NUM, &pbtBuff[inOffset]);

		inOffsetBefore = inOffset;
		inOffset += inAddDataToEMVPacket(TAG_9F26_APPL_CRYPTOGRAM, &pbtBuff[inOffset]);	
		inSize = inOffset - inOffsetBefore;
		if (inSize>0){ // : Store for Batch Upload
			inStoreBatchFieldData ((BATCH_REC *)&pobTran->srBRec, FID_EMV_TAG_9F26_APP_CRYPTOGRAM_LEN_REQ, (unsigned char*)&inSize, sizeof(int));
			inStoreBatchFieldData ((BATCH_REC *)&pobTran->srBRec, FID_EMV_TAG_9F26_APP_CRYPTOGRAM_REQ,  (unsigned char*)&pbtBuff[inOffsetBefore], inSize);
		}
		inOffset += inAddDataToEMVPacket(TAG_9F27_CRYPT_INFO_DATA, &pbtBuff[inOffset]);
		inOffset += inAddDataToEMVPacket(TAG_9F33_TERM_CAP, &pbtBuff[inOffset]);
		inOffset += inAddDataToEMVPacket(TAG_9F34_CVM_RESULTS, &pbtBuff[inOffset]);
		inOffset += inAddDataToEMVPacket(TAG_9F35_TERM_TYPE, &pbtBuff[inOffset]);
		inOffset += inAddDataToEMVPacket(TAG_9F36_ATC, &pbtBuff[inOffset]);
		inOffset += inAddDataToEMVPacket(TAG_9F37_UNPRED_NUM, &pbtBuff[inOffset]);
		inOffset += inAddDataToEMVPacket(TAG_9F41_TRANS_SEQ_COUNTER, &pbtBuff[inOffset]);
		inOffset += inAddDataToEMVPacket(TAG_9F53_TRAN_CURR_CODE, &pbtBuff[inOffset]);
		//jrd 05182005
		inOffset += inAddDataToEMVPacket(TAG_5F2A_TRANS_CURCY_CODE, &pbtBuff[inOffset]);
		//edi
		inOffset += inAddDataToEMVPacket(TAG_5F34_APPL_PAN_SEQNUM, &pbtBuff[inOffset]);
		inOffset += inAddDataToEMVPacket(TAG_84_DF_NAME, &pbtBuff[inOffset]);

		//LeeChyi - 29122050: START
		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_ISSUER_SCRIPT_RESULTS_LEN);
//		if (inSize > 0)
//			inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_ISSUER_SCRIPT_RESULTS, inSize, &pbtBuff[inOffset]);
		if (inSize > 0)
		{
			unsigned char chScriptTmp[25];
			inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_ISSUER_SCRIPT_RESULTS, inSize, chScriptTmp);
			inOffset +=inBuildTLVfromData(&pbtBuff[inOffset], 0x9F5B, inSize, chScriptTmp);
		}
		//LeeChyi - END


		*pusLen = (unsigned short)inOffset;
	}
	else if((pobTran->srBRec.inCode == BATCH_UPLOAD) ||
		(pobTran->srTRTRec.TransactionCode == PRE_COMP)||
		(pobTran->srTRTRec.TransactionCode == VOID) ||
		(pobTran->srBRec.inCode == TC_UPLOAD)
		) {
		LOG_PRINTF(("DE55-3"));
		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_82_AIP_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_82_AIP, inSize, &pbtBuff[inOffset]);

		//inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_84_DF_NAME_LEN);
		//inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_84_DF_NAME, inSize, &pbtBuff[inOffset]);

		//9F10,95,91,9F26
		if (pobTran->srBRec.inCode == BATCH_UPLOAD){
#ifdef IMAM_PERMATA
			inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_91_LEN);
			inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_91, inSize, &pbtBuff[inOffset]);

			inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_95_TVR_LEN_REQ);
			inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_95_TVR_REQ, inSize, &pbtBuff[inOffset]);
#endif
		} else {
			//inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_95_TVR_LEN);
			//inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_95_TVR, inSize, &pbtBuff[inOffset]);
			//inPrintWatchPacket((unsigned char*) &pbtBuff[inOffset-inSize], inSize,"joey2");
		}

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9A_TXN_DATE_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9A_TXN_DATE, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9C_TXN_TYPE_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9C_TXN_TYPE, inSize, &pbtBuff[inOffset]);

		//inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_5F2A_CURR_CODE_LEN);
		//inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_5F2A_CURR_CODE, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F02_AMT_AUTH_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F02_AMT_AUTH, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F03_AMT_OTHR_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F03_AMT_OTHR, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F09_APPLN_VER_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F09_APPLN_VER, inSize, &pbtBuff[inOffset]);

#ifdef IMAM_PERMATA
		if (pobTran->srBRec.inCode == BATCH_UPLOAD){
			LOG_PRINTFF((0x0L, "TAG_9F10 : NOT SENT in VOID, etc")); //LIEM
			//inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F10_ISSUER_APP_DATA_LEN);
			//inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F10_ISSUER_APP_DATA, inSize, &pbtBuff[inOffset]);
			inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F10_ISSUER_APP_DATA_LEN_REQ);
			inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F10_ISSUER_APP_DATA_REQ, inSize, &pbtBuff[inOffset]);
		}
#endif

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F1A_TERM_CRTY_CODE_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F1A_TERM_CRTY_CODE, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F1E_IFD_SER_NUM_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F1E_IFD_SER_NUM, inSize, &pbtBuff[inOffset]);

#ifdef IMAM_PERMATA
		if (pobTran->srBRec.inCode == BATCH_UPLOAD){
			inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F26_APP_CRYPTOGRAM_LEN_REQ);
			inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F26_APP_CRYPTOGRAM_REQ, inSize, &pbtBuff[inOffset]);
		} else 
#endif
		{
			inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F26_APP_CRYPTOGRAM_LEN);
			inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F26_APP_CRYPTOGRAM, inSize, &pbtBuff[inOffset]);
		}

		//inPrintWatchPacket((unsigned char*) &pbtBuff[inOffset-inSize], inSize,"joey11");
		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F27_CID_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F27_CID, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F33_TERM_CAPABILITIES_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F33_TERM_CAPABILITIES, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F34_CVM_RESULTS_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F34_CVM_RESULTS, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F35_TERM_TYPE_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F35_TERM_TYPE, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F36_ATC_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F36_ATC, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F37_UNPREDICT_NUMBER_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F37_UNPREDICT_NUMBER, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F41_TRANS_SEQ_COUNTER_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F41_TRANS_SEQ_COUNTER, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_9F53_TRAN_CAT_CODE_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_9F53_TRAN_CAT_CODE, inSize, &pbtBuff[inOffset]);

		//jrd 05232005
		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_5F2A_CURR_CODE_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_5F2A_CURR_CODE, inSize, &pbtBuff[inOffset]);

		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_TAG_84_DF_NAME_LEN);
		inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_TAG_84_DF_NAME, inSize, &pbtBuff[inOffset]);

		// commented out for spqalog038
		
		//LeeChyi - 29122050 - START
		inSize = inGetDataLengthFromBatch(pobTran, FID_EMV_ISSUER_SCRIPT_RESULTS_LEN);
//		if (inSize > 0)
//			inOffset += inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_ISSUER_SCRIPT_RESULTS, inSize, &pbtBuff[inOffset]);
		if (inSize > 0)
		{
			unsigned char chScriptTmp[25];
			inGetDataFromBatchToEMVPacket(pobTran, FID_EMV_ISSUER_SCRIPT_RESULTS, inSize, chScriptTmp);
			inOffset +=inBuildTLVfromData(&pbtBuff[inOffset], 0x9F5B, inSize, chScriptTmp);
		}
		//LeeChyi - END

		*pusLen = (unsigned short)inOffset;
	}

	return VS_SUCCESS;
}

//Added by Thiru
int inBuildTLVfromData(unsigned char *pbtPackBuff, unsigned short usTag, unsigned short usTagLen, unsigned char *chBuffer)
{
	int  offset = 0;
	
	if ((usTag & 0x1F00) == 0x1F00)
	{	// 2-byte tag
		pbtPackBuff[offset++] = (byte) (usTag >> 8);
		pbtPackBuff[offset++] = (byte) (usTag & 0x00FF);
	}
	else
	{
		pbtPackBuff[offset++] = (byte) (usTag >> 8);
	}

	pbtPackBuff[offset++] = (byte) usTagLen;
	
	memcpy(pbtPackBuff + offset, chBuffer, usTagLen);

	return(offset + usTagLen);	// Return number of bytes added to buffer
}

int inAddDataToEMV_AMEXPacket(unsigned short usTag, unsigned char *pbtPackBuff) //edi - AMEX EMV
{
	unsigned short usLen;
	byte btTmp[128];
	int  offset = 0;
	char buff[256];

	if (usEMVGetTLVFromColxn(usTag, btTmp, &usLen) != EMV_SUCCESS) {
		return(0);	// Do does not exist so nothing added to packet
	} else {
		memset(buff, 0x00, sizeof(buff));
		if (usLen<128) hex_2_asc((BYTE *)buff, btTmp, usLen);
		LOG_PRINTFF((0x08L, "TAG(0x%X): EMVGetTLV[??? = %s] -- in inAddDataToEMVPacket", usTag, buff));
	}

	memcpy(pbtPackBuff + offset, btTmp, usLen);

	return(offset + usLen);	// Return number of bytes added to buffer
}

int inAddDataToEMV_AMEXPacket2(unsigned short usTag, unsigned char *pbtPackBuff)
{
	unsigned short usLen;
	byte btTmp[128];
	int  offset = 0;
	char buff[256];

	if (usEMVGetTLVFromColxn(usTag, btTmp, &usLen) != EMV_SUCCESS) {
		return(0);	// Do does not exist so nothing added to packet
	} else {
		memset(buff, 0x00, sizeof(buff));
		if (usLen<128) hex_2_asc((BYTE *)buff, btTmp, usLen);
		LOG_PRINTFF((0x08L, "TAG(0x%X): EMVGetTLV[??? = %s] -- in inAddDataToEMVPacket", usTag, buff));
	}


	pbtPackBuff[offset++] = (byte) usLen;

	memcpy(pbtPackBuff + offset, btTmp, usLen);

	return(offset + usLen);	// Return number of bytes added to buffer
}

int inAddDataToEMVPacket(unsigned short usTag, unsigned char *pbtPackBuff)
{
	unsigned short usLen;
	byte btTmp[128];
	int  offset = 0;
	char buff[256];

	if (usEMVGetTLVFromColxn(usTag, btTmp, &usLen) != EMV_SUCCESS) {
		return(0);	// Do does not exist so nothing added to packet
	} else {
		memset(buff, 0x00, sizeof(buff));
		if (usLen<128) hex_2_asc((BYTE *)buff, btTmp, usLen);
		LOG_PRINTFF((0x08L, "TAG(0x%X): EMVGetTLV[??? = %s] -- in inAddDataToEMVPacket", usTag, buff));
	}

	if ((usTag & 0x1F00) == 0x1F00)
	{	// 2-byte tag
		pbtPackBuff[offset++] = (byte) (usTag >> 8);
		pbtPackBuff[offset++] = (byte) (usTag & 0x00FF);
	}
	else
	{
		pbtPackBuff[offset++] = (byte) (usTag >> 8);
	}

	pbtPackBuff[offset++] = (byte) usLen;

	memcpy(pbtPackBuff + offset, btTmp, usLen);

	return(offset + usLen);	// Return number of bytes added to buffer
}

/*********************************************************************************************
* @func int | inEMVPIPAnalyse |
*       Function for the PACKET_OBJECT structure. Stores the response message and uploads
*		the advices
*
* @parm PACKET_OBJECT * | obPacket
*	    Packet object
*
* @rdesc return values.
* @flag int | VS_SUCCESS or VS_ERR
* @end
**********************************************************************************************/

int inEMVPIPAnalyse  (PACKET_OBJECT *pobPacket)
{
   	char szAuthCode[AUTH_CODE_SIZE + 1];
    char szStatusMessage[MESSAGE_SIZE + 1];
    unsigned char szReqMti[MTI_SIZE];   /* CPAC_UNBKL added */
	unsigned char szDummy[50];			/* CPAC_UNBKL added */
	TRANSACTION_OBJECT *pobTran;

	/* pdebug(("--inCPACPIPAnalyse--")); */
 	pobTran = (TRANSACTION_OBJECT *)pobPacket->Transaction;

 /* get data from field table fields and put in GDS
    this needs to happen before inUploadAdvices() */
    inPFRStateStoreAll(CPAC_PFR_ANALYSE_BEGIN ,pobTran,1,1);


if (!fGetOffline(pobTran))
    {

// CPAC_UNBKL added {
        inLoadBitmap( ((TRANSACTION_OBJECT*)pobPacket->Transaction)->srBRec.inCode, szReqMti, szDummy, szDummy );
		szReqMti[1]+=0x10;
		if( memcmp( szReqMti, Mti, MTI_SIZE ) != 0 )
		{
			ACTIVITY_LOG("MTI mismatch!");

			#ifdef VXEMVAPP
				pobTran->inOnlineFailed = EMV_COMMS_FAILURE;
				return (VS_SUCCESS);
			#else
				return( VS_ERR );
			#endif /* VXEMVAPP */
		}
// CPAC_UNBKL added }

#ifdef VXEMVAPP
	if((pobTran->inChipStatus == EMV_CARD)&&(pobTran->inOnlineFailed == EMV_COMMS_FAILURE))
	{
		return (VS_SUCCESS);
	}
#endif /* VXEMVAPP */



	strlcpy(pobTran->srBRec.szAuthCode, (char *)srISO.pszField38, FIELD38_SIZE); /* Auth Code to Batch */

        strlcpy(pobTran->srBRec.szRespCode, (char *)srISO.pszField39, FIELD39_SIZE); /* Response Code to Batch */

	//Update collection with the response code from host
	usEMVUpdateTLVInCollxn(TAG_8A_AUTH_RESP_CODE , (byte *)srISO.pszField39, 2);
	LOG_PRINTFF((0x08L, "TAG(0x%X): EMVUpdateTLV[TAG_8A_AUTH_RESP_CODE = %s]", TAG_8A_AUTH_RESP_CODE, srISO.pszField39));

        /* Host date & time to pobTran */
        strlcpy(pobTran->srBRec.szTime, (char *)srISO.pszField12, TIME_SIZE);
        strlcpy(pobTran->srBRec.szDate, (char *)srISO.pszField13, DATE_SIZE);

	 vdCPACSyncHostDateTime(pobTran->srBRec.szDate, pobTran->srBRec.szTime);
    }

#ifdef VXEMVAPP
        if (strcmp(pobTran->srBRec.szRespCode, "00") == 0)
        {
            pobTran->srBRec.inTransactionResult = AUTHORIZED;
            vdSGErrorMessage(0L);
            vdSGRespMsg("Approval");
    	}
    	/*else if ((strcmp(pobTran->srBRec.szRespCode, "01") == 0) ||
            (strcmp(pobTran->srBRec.szRespCode, "02") == 0))
        {
		pobTran->inEMVCardDecision = EMV_VOICE_REFERRAL;
            	pobTran->srBRec.inTransactionResult = HOST_REFERRAL;
            	vdSGErrorMessage(0L);
            	vdSGRespMsg("Referral");
    	}
    	else if (strcmp(pobTran->srBRec.szRespCode, "05") == 0) //To check this .....
        {
            	pobTran->srBRec.inTransactionResult = HOST_DECLINED;
            	vdSGErrorMessage(TRANS_FAIL_ERR_MSG21);
            	vdSGRespMsg("Declined");
    	}
    	else
    	{
    	    	pobTran->inOnlineFailed = EMV_COMMS_FAILURE;  //To check this
            	pobTran->srBKey.fOffline = VS_TRUE;
        }*/

	//inGlobalNumberScripts = createScriptFiles(pobPacket->pchReceiveBuff);
	#endif /* VXEMVAPP */

    if ( (!fGetOffline(pobTran)) && (pobTran->srBRec.inType != ADMIN_TRAN) &&  inGetNumAdv() )
// CPAC_UNBKL added inGetHDTNumAdv()
    {
        inUploadCPACAdvices(VS_FALSE, pobTran);
    }

   memset(szStatusMessage,0,sizeof(szStatusMessage));

   vdGetMessageFromFile( TRANS_APP_MSG, szStatusMessage);
   strlcpy(szAuthCode, pobTran->srBRec.szAuthCode, AUTH_CODE_SIZE );

#if CPAC_UNBKL_REMOVED
   if ( inGGCode() != REFUND_OFFLINE)   /* Do not display zeroes */
   {
    strcat(szStatusMessage, "  ");
    strcat(szStatusMessage, szAuthCode);
   }
#endif /* CPAC_UNBKL_REMOVED */

   vdSGRespMsg(szStatusMessage);

	return(VS_SUCCESS);
}

int inGetDataFromBatchToEMVPacket(TRANSACTION_OBJECT *pobTran, unsigned int uiFieldID, int inSize, unsigned char *pbtBuff)
{
	//int inLen;
	unsigned char szTmp[255];

	memset(szTmp, 0x00, sizeof(szTmp));
	if(inGetBatchFieldData(&pobTran->srBRec, uiFieldID, szTmp, inSize) != VS_ERR){
		memcpy(pbtBuff, szTmp, inSize);
		return inSize;
	} else {
		return 0;
		//return VS_ERR; //
	}
}
int inMakeAmexEMVBatchData1(unsigned short usTag, unsigned char *pbtPackBuff) //edi - AMEX EMV
{
	unsigned short usLen;
	byte btTmp[128];
	int  offset = 0;
	char buff[256];

	if (usEMVGetTLVFromColxn(usTag, btTmp, &usLen) != EMV_SUCCESS) {
		return(0);	// Do does not exist so nothing added to packet
	} else {
		memset(buff, 0x00, sizeof(buff));
		if (usLen<128) hex_2_asc((BYTE *)buff, btTmp, usLen);
		LOG_PRINTFF((0x08L, "TAG(0x%X): EMVGetTLV[??? = %s] -- in inAddDataToEMVPacket", usTag, buff));
	}

	memcpy(pbtPackBuff + offset, btTmp, usLen);

	return(offset + usLen);	// Return number of bytes added to buffer
}

int inMakeAmexEMVBatchData2(unsigned short usTag, unsigned char *pbtPackBuff)
{
	unsigned short usLen;
	byte btTmp[128];
	int  offset = 0;
	char buff[256];

	if (usEMVGetTLVFromColxn(usTag, btTmp, &usLen) != EMV_SUCCESS) {
		return(0);	// Do does not exist so nothing added to packet
	} else {
		memset(buff, 0x00, sizeof(buff));
		if (usLen<128) hex_2_asc((BYTE *)buff, btTmp, usLen);
		LOG_PRINTFF((0x08L, "TAG(0x%X): EMVGetTLV[??? = %s] -- in inAddDataToEMVPacket", usTag, buff));
	}


	pbtPackBuff[offset++] = (byte) usLen;

	memcpy(pbtPackBuff + offset, btTmp, usLen);

	return(offset + usLen);	// Return number of bytes added to buffer
}

int inMakeEMVBatchData(unsigned short usTag, unsigned char *pbtPackBuff)
{
	unsigned short usLen;
	byte btTmp[128];
	int  offset = 0;
	char buff[256];

	if (usEMVGetTLVFromColxn(usTag, btTmp, &usLen) != EMV_SUCCESS) {
		return(0);	// Do does not exist so nothing added to packet
	} else {
		memset(buff, 0x00, sizeof(buff));
		if (usLen<128) hex_2_asc((BYTE *)buff, btTmp, usLen);
		LOG_PRINTFF((0x08L, "TAG(0x%X): EMVGetTLV[??? = %s] -- in inMakeEMVBatchData", usTag, buff));
	}

	if ((usTag & 0x1F00) == 0x1F00)
	{	// 2-byte tag
		pbtPackBuff[offset++] = (byte) (usTag >> 8);
		pbtPackBuff[offset++] = (byte) (usTag & 0x00FF);
	}
	else
	{
		pbtPackBuff[offset++] = (byte) (usTag >> 8);
	}

	pbtPackBuff[offset++] = (byte) usLen;

	memcpy(pbtPackBuff + offset, btTmp, usLen);

	return(offset + usLen);	// Return number of bytes added to buffer

}

int inGetDataLengthFromBatch(TRANSACTION_OBJECT *pobTran, unsigned int uiFieldID)
{
	int inSize=0;

	if(inGetBatchFieldData(&pobTran->srBRec, uiFieldID, (unsigned char*)&inSize, sizeof(int)) != VS_ERR) {
		return inSize;
	} else {
		return 0;
		//return VS_ERR;  //
	}
}

/*********************************************************************************************
* @func void | vdEMVLoadField38 |
* Load the Response Auth Code - ISO Field # 38, This is done for Offline Refund
*
* @parm TRANSACTION_OBJECT * | pobTran |
*       This is the transaction object.
*
* @end
**********************************************************************************************/

void vdEMVLoadField38 (TRANSACTION_OBJECT *pobTran)
{
   int     inTranCode;

   inTranCode = pobTran->srBRec.inCode;

   if (inTranCode == REFUND && fGetOffline(pobTran) && (pobTran->srBRec.inChipStatus != EMV_CARD))
   {
       MEMCLR (pobTran->srBRec.szAuthCode, AUTH_CODE_SIZE );
       memset (pobTran->srBRec.szAuthCode, '0', AUTH_CODE_SIZE);
       memset ((char *)srISO.pszField38, '0', AUTH_CODE_SIZE);
   }
   else
   {
        strlcpy((char *)srISO.pszField38, pobTran->srBRec.szAuthCode, AUTH_CODE_SIZE);
        pad((char *)srISO.pszField38,(char *)srISO.pszField38,SPACE,AUTH_CODE_SIZE,LEFT);
    }
}
